library(pacman)
p_load(sf,raster,dplyr)


if (Sys.info()[['user']]=='mineria.ra'){
  main_path<-'C:/Users/Mineria.ra/Dropbox/Research/PES Chiapas RCT/CreatedData' 
  deforested_draw<- "Deforested_IPA"
}else if (Sys.info()[['user']]=='santiago.fernandez'){
  main_path <- "H:/Mi unidad/CreatedData"
  property_files<- 'Final_shapes'
  deforested_draw<- "Deforested_IPA"
}else if (Sys.info()[['user']]=='LENOVO'){
  main_path<-'C:/Users/LENOVO/Dropbox/RA/PES Chiapas RCT/CreatedData' 
  deforested_draw<- "Final_shapes"
  deforested_draw<- "Deforested_IPA"
}


setwd(main_path)

master<- read.csv("Lacandona_contracts.csv",sep=";")%>%
  mutate(Total_Property_folder_1=gsub("\\\\","/",Total_Property_folder_1))

levels(master$Deforested_IPA)[match("def_pol_4508_1422.shp",levels(master$Deforested_IPA))] <- "def_pol_4508_142.shp"
levels(master$Deforested_IPA)[match("def_pol_4508_1421.shp",levels(master$Deforested_IPA))] <- "def_pol_4508_142.shp"

## This function makes all the shapes from intersection on draws, property and
## Contracts

process_shape <- function(i){
  setwd(main_path)

  contract<-st_read(paste(paste("Conafor_2021",master$Contract_property_folder[i],
                                master$Contract_property_file[i],
                                sep="/"),".shp",sep=""),quiet = T)
  contract<-st_make_valid(st_transform(contract,crs="WGS84"))
  
  shape_draw<-st_read(paste("Deforested_IPA",
                            master$Deforested_IPA[i],sep="/"),quiet = T)
  shape_draw<-st_make_valid(st_transform(shape_draw,crs="WGS84"))
  
  mapview::mapView(contract$geometry)+mapview::mapView(shape_draw)
  sf::sf_use_s2(FALSE)

  contract_land<-st_difference(contract,shape_draw)
  
  plot(contract$geometry)
  plot(shape_draw$geometry,add=T)
  plot(contract_land$geometry,add=T,border='red')
  
  write_sf(contract_land     ,paste("Conafor_2021/Final_shapes/Contracted/"    
                                    ,gsub("def_","",master$Deforested_IPA[i]),sep=""),overwrite=TRUE)

}


for (i in c(3,31,32,33,37,45,58,64,68)){
  process_shape(i)
}

process_shape_nodef <- function(i){
  setwd(main_path)
  
  #total<-st_read(paste(paste("Conafor_2021",wd_total,total_file,sep="/"),".shp",sep=""))
  #total<-st_make_valid(st_transform(total,crs="WGS84"))
  
  contract<-st_read(paste(paste("Conafor_2021",master$Total_Property_folder[i],
                                master$Total_Property_file[i],
                                sep="/"),".shp",sep=""),quiet = T)
  #contract<-st_make_valid(st_transform(contract,crs="WGS84"))
  
  #shape_draw<-st_read(paste("Deforested_IPA",
                            #master$Deforested_IPA[i],sep="/"),quiet = T)
  #shape_draw<-st_make_valid(st_transform(shape_draw,crs="WGS84"))
  
  #sf::sf_use_s2(FALSE)
  #non_contract_land<-st_difference(total,contract)
  #non_contract_land<-st_intersection(non_contract_land,shape_draw)
  
  #contract_land<-st_difference(contract,shape_draw)
  
  #plot(contract$geometry)
  #plot(shape_draw$geometry,add=T)
  #plot(contract_land$geometry,add=T,border='red')
  
  #property<-st_intersection(total,shape_draw)
  
  #write_sf(property          ,paste("Predictions/Shapes/Property/"    
  #                                ,gsub("def_","",defo_file),sep=""),overwrite=TRUE)
  write_sf(contract     ,paste("Conafor_2021/Final_shapes/Contracted/"    
                                    ,gsub("def_","",master$Deforested_IPA[i]),sep=""),overwrite=TRUE)
  #write_sf(non_contract_land ,paste("Predictions/Shapes/non_Contract/"
  #                                 ,gsub("def_","",defo_file),sep=""),overwrite=TRUE)
}

master$Total_Property_folder[79]<-"Final_shapes\Shapes_IPA"


for (i in 5:nrow(master)){
    process_shape(i)
    print(i)
}

for (i in 31:32){
  process_shape(i)
}
  
  

for (i in c(2,30,36,44,57,63,67)){
  process_shape_nodef(i)
}

for (i in c(2,30,36,44,57,63,67)){
  print(master$Total_Property_folder[i])
}

write.csv(master,"Copia de Lacandona_contracts_231122.xlsx - Sheet1.csv",row.names = F)

